<?php

namespace Core\Forms\Init;

use Core\Validation\Validator\FunctionValidator;
use Phalcon\Di;
use Phalcon\Forms\ElementInterface;
use Phalcon\Validation\Validator\Alnum;
use Phalcon\Validation\Validator\CreditCard;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Identical;
use Phalcon\Validation\Validator\Digit;
use Phalcon\Validation\Validator\ExclusionIn;
use Phalcon\Validation\Validator\InclusionIn;
use Phalcon\Validation\Validator\Date;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\Url;
use Phalcon\Validation\Validator\File;
use Phalcon\Validation\Validator\Regex;
use Phalcon\Validation\Validator\StringLength;
use Phalcon\Validation\Validator\Between;
use Phalcon\Validation\Validator\Confirmation;

class Validation
{
    public static function presenceOf(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '该值不能为空';
        }
        $t->setAttribute('required', 'true');
        $t->setAttribute('data-bv-notempty-message', $validate['message']);
        $t->addValidator(new PresenceOf($validate));
    }

    public static function alnum(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '只允许字母或者数字';
        }
        $t->setAttribute('pattern', '[a-z][0-9]{1,}');
        $t->setAttribute('data-bv-regexp-message', $validate['message']);
        $t->addValidator(new Alnum($validate));
    }

    public static function url(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '只允许字母或者数字';
        }
        $t->setAttribute('pattern', '[a-z][0-9]{1,}');
        $t->setAttribute('data-bv-regexp-message', $validate['message']);
        $t->addValidator(new Url($validate));
    }

    public static function digit(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '只允许数字字符串';
        }
        $t->setAttribute('pattern', '[0-9]{1,}');
        $t->setAttribute('data-bv-regexp-message', $validate['message']);
        $t->addValidator(new Digit($validate));
    }

    public static function date(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '只允许合法的日期格式';
        }
        $t->setAttribute('data-bv-date', 'true');
        if (isset($validate['jsformat'])) {
            $t->setAttribute('data-bv-date-format', $validate['jsformat']);
        }
        $t->setAttribute('data-bv-date-message', $validate['message']);
        $t->addValidator(new Date($validate));
    }

    public static function creditCard(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '只允许合法的信用卡号码';
        }
        $t->setAttribute('data-bv-creditcard', 'true');
        $t->setAttribute('data-bv-creditcard-message', $validate['message']);
        $t->addValidator(new CreditCard($validate));
    }

    public static function file(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '不可以上传非法文件哦';
        }

        if (isset($validate['allowedTypes'])) {
            if (is_array($validate['allowedTypes'])) {
                $allowedTypes = implode(',', $validate['allowedTypes']);
            } else {
                $allowedTypes = $validate['allowedTypes'];
            }
            $t->setAttribute(' 	data-bv-file-type', $allowedTypes);
        }

        if (isset($validate['maxSize'])) {
            $t->setAttribute('data-bv-file-maxsize', $validate['maxSize']);
        }

        if (isset($validate['minSize'])) {
            $t->setAttribute('data-bv-file-minsize', $validate['minSize']);
        }

        if (isset($validate['extension'])) {
            $attributeExtension = false;

            if (is_array($validate['extension'])) {
                $attributeExtension = implode(',', $validate['extension']);
            } elseif (is_string($attributeExtension)) {
                $attributeExtension = $validate['extension'];
            }

            if ($attributeExtension !== false) {
                $t->setAttribute('data-bv-file-extension', $attributeExtension);
            }
        }

        $t->setAttribute('data-bv-file-message', $validate['message']);
        $t->addValidator(new File($validate));
    }

    public static function identical(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '您必须要接受这个选项才能继续。';
        }
        $t->setAttribute('required', 'true');
        $t->setAttribute('data-bv-notempty-message', $validate['message']);
        $t->addValidator(new Identical($validate));
    }

    public static function functions(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '验证失败。';
        }
        if (isset($validate['remote'])) {
            $remote = $validate['remote'];
            if (isset($remote['url'])) {
                $t->setAttribute('data-bv-remote-url', $remote['url']);
            } else {
                $t->setAttribute('data-bv-remote-url', Di::getDefault()->getShared('url')->get(['for' => 'functionValidation', 'callable' => $validate['callable']]));
            }

            if (isset($remote['name'])) {
                $t->setAttribute('data-bv-remote-name', $remote['name']);
            } else {
                $t->setAttribute('data-bv-remote-name', $t->getName());
            }

            if (isset($remote['data'])) {
                $t->setAttribute('data-bv-remote-delay', json_encode($remote['data']));
            }

            $t->setAttribute('data-bv-remote-message', $validate['message']);

        }
        $t->addValidator(new FunctionValidator($validate));
    }

    public static function email(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '邮件地址不合法';
        }
        $t->setAttribute('data-bv-emailaddress-message', $validate['message']);
        $t->addValidator(new Email($validate));
    }

    public static function exclusionIn(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '值不能包含' . implode('，', $validate['domain']);
        }
        $t->addValidator(new ExclusionIn($validate));
    }

    public static function inclusionIn(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '值必须是以下值其中之一：' . implode('，', $validate['domain']);
        }
        $t->addValidator(new InclusionIn($validate));
    }

    public static function regex(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '字段数据不合法';
        }
        $t->setAttribute('pattern', $validate['pattern']);
        $t->setAttribute('data-bv-regexp-message', $validate['message']);
        $t->addValidator(new Regex($validate));
    }

    public static function stringLength(ElementInterface &$t, $validate)
    {
        if (!isset($validate['messageMaximum']) || empty($validate['messageMaximum'])) {
            $validate['messageMaximum'] = '值的长度不能大于' . $validate['max'];
        }
        if (!isset($validate['messageMinimum']) || empty($validate['messageMinimum'])) {
            $validate['messageMinimum'] = '值的长度不能小于' . $validate['min'];
        }
        if (isset($validate['max'])) {
            $t->setAttribute('maxlength', strval($validate['max']));
        }
        if (isset($validate['min'])) {
            $t->setAttribute('minlength', strval($validate['min']));
        }

        $t->addValidator(new StringLength($validate));
    }

    public static function between(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '数值必须在　' . $validate['minimum'] . '　和　' . $validate['maximum'] . '　之间';
        }
        $t->setAttribute('min', $validate['minimum']);
        $t->setAttribute('max', $validate['maximum']);
        $t->setAttribute('data-bv-between-message', $validate['message']);
        $t->addValidator(new Between($validate));
    }

    public static function confirmation(ElementInterface &$t, $validate)
    {
        if (!isset($validate['message']) || empty($validate['message'])) {
            $validate['message'] = '值不合法';
        }
        if (is_array($validate['with'])) {
            $with = end($validate['with']);
        } else {
            $with = $validate['with'];
        }
        $t->setAttribute('data-bv-identical', 'true');
        $t->setAttribute('data-bv-identical-field', $with);
        $t->addValidator(new Confirmation($validate));
    }
}
